package Aaaaaa;

public class Main7 {
    public static void main(String[] args) {
//        System.out.println(dec("Def H",-1,1));
        System.out.println(get_substr("222BCC111CB"));
    }
    public static String dec (String str, int up_chr_offset, int low_chr_offset) {
        // write code here
        int n=up_chr_offset;
        int m=low_chr_offset;
        StringBuilder res=new StringBuilder();
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)>'A'&&str.charAt(i)<'Z'){
                res.append((char)(str.charAt(i)+n));
            }else if(str.charAt(i)>'a'&&str.charAt(i)<'z'){
                res.append((char)(str.charAt(i)+m));
            }else{
                res.append(str.charAt(i));
            }
        }
        return res.toString();
    }

    public static String get_substr (String st) {
        // write code here
        /**
         * "222BCC111CB"
         * "BB"
         */
        StringBuilder s=new StringBuilder(st.charAt(0)+"");
        int len=st.length();
        int[] temp=new int[128];
        temp[st.charAt(0)]=1;
        for(int i=1;i<len;i++){
            if(s.length()==0||st.charAt(i)!=s.charAt(s.length()-1)){
                s.append(st.charAt(i)+"");
                temp[st.charAt(i)]++;
            }else if(st.charAt(i)==s.charAt(s.length()-1)){
                while(st.charAt(i)==s.charAt(s.length()-1)) {
                    temp[st.charAt(i)]++;
                    i++;
                }
                if(temp[s.charAt(s.length()-1)]>=3){
                    s.delete(s.length()-1,s.length());
                }else{
                    for(int j=0;j<temp[st.charAt(i)];j++){
                        s.append(s.charAt(s.length()-1)+"");
                    }
                }
                temp[st.charAt(i)]=0;
            }
        }
        return s.toString();
    }
}
